//
//  ViewController.swift
//  ziyan
//
//  Created by pencilCool on 2021/8/28.
//

import UIKit
    
import Vision
import VisionKi
import SnapKit
//14383A
class ViewController: UIViewController {
    
    @IBOutlet weak var takePhotoButton: UIButton!
    
    @IBOutlet weak var resultImageView: UIImageView!

     var textRecognitionRequest = VNRecognizeTextRequest()
    
    override func viewDidLoad() {
        super.viewDidLoad()

             textRecognitionRequest = VNRecognizeTextRequest(completionHandler: { (request, error) in
            if let error = error {
                print("ERROR: \(error)")
                return
            }
            guard let results = request.results, results.count > 0 else {
                print("No text found")
                return
            }

            for result in results {
                if let observation = result as? VNRecognizedTextObservation {
                    for text in observation.topCandidates(1) {
                        print(text.string)
                        print(text.confidence)
                        print(observation.boundingBox)
                        print("\n")
                    }
                }
            }
        })
        
        textRecognitionRequest.recognitionLevel = .accurate
        textRecognitionRequest.usesLanguageCorrection = true
        textRecognitionRequest.recognitionLanguages = ["zh-Hans"]

    }


    
    @IBAction func takePhoto(_ sender: Any) {
        
        
    }

      
    func processImage(image: UIImage) {
        guard let cgImage = image.cgImage else {
            print("Failed to get cgimage from input image")
            return
        }
        
        let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
        do {
            try handler.perform([textRecognitionRequest])
        } catch {
            print(error)
        }
    }
   
}
